package com.atguigu.service.impl;

import com.atguigu.client.VodClient;
import com.atguigu.entity.EduVideo;
import com.atguigu.mapper.EduVideoMapper;
import com.atguigu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入VodClient
    @Autowired
    private VodClient vodClient;

    /**
     * 根据课程id删除小节
     * 删除小节，删除对应的视频文件
     * @param courseId
     **/
    @Override
    public void removeVideoByCourseId(String courseId) {
        //1、根据课程id查询课程所有的视频id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        //wrapper.select("video_source_id");

        List<EduVideo> eduVideoList = baseMapper.selectList(wrapper);

        //List<EduVideo> --> List<String>
        List<String> videoIds = new ArrayList<>();
        for (EduVideo video : eduVideoList) {
            String videoSourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }

        }

        //多个视频id删除视频
        if(videoIds.size() > 0){
            vodClient.removeMoreVideo(videoIds);
        }

        baseMapper.delete(wrapper);
    }
}
